package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.IRoomPerConsumptionDao;
import com.eling.elcms.travel.model.RoomPerConsumption;
import com.eling.elcms.travel.model.RoomPersonalInfo;
import com.eling.elcms.travel.model.view.ConsumpSaveView;
import com.eling.elcms.travel.service.IRoomPerConsumptionManager;
import com.eling.elcms.travel.service.IRoomPersonalInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public class RoomPerConsumptionManagerImpl extends GenericManagerImpl<RoomPerConsumption, Long>
		implements IRoomPerConsumptionManager {

	IRoomPerConsumptionDao roomPerConsumptionDao;

	@Autowired
	private IRoomPersonalInfoManager roomPersonalInfoManager;

	@Autowired
	public void setRoomPerConsumptionDao(IRoomPerConsumptionDao roomPerConsumptionDao) {
		this.dao = roomPerConsumptionDao;
		this.roomPerConsumptionDao = roomPerConsumptionDao;
	}

	@Override
	public RoomPerConsumption saveConsump(ConsumpSaveView view) {
		Long pkRoomPer = view.getPkRoomPersonalInfo();
		RoomPersonalInfo roomPerInfo = roomPersonalInfoManager.get(pkRoomPer);
		if (view.getConsumptionList() != null && !view.getConsumptionList().isEmpty()) {
			Set<RoomPerConsumption> roomPerConSet = new HashSet<>(save(view.getConsumptionList()));
			roomPerInfo.setRoomPerConsumptions(roomPerConSet);
			roomPersonalInfoManager.save(roomPerInfo);
		} else {
			roomPerInfo.setRoomPerConsumptions(null);
			roomPersonalInfoManager.save(roomPerInfo);
		}
		return new RoomPerConsumption();
	}

}
